package org.gwtmultipage.rebind.selector;

import com.google.gwt.core.ext.typeinfo.JClassType;
import org.gwtmultipage.client.UrlPatternEntryPoint;
import org.gwtmultipage.rebind.EntryPointFactoryImplGenerator;

/**
 * @author breilly
 */
public class UrlPatternEntryPointSelector extends BaseEntryPointSelector {

	public boolean canSelect(JClassType classType) {
		return getAnnotation(classType) != null;
	}

	@Override
	protected String generateMatchCondition(JClassType classType) {
		UrlPatternEntryPoint annotation = getAnnotation(classType);
		if (annotation != null) {
			return EntryPointFactoryImplGenerator.RELATIVE_PATH_VAR + ".matches(\"" + annotation.value() + "\")";
		} else {
			throw new RuntimeException(classType.getQualifiedSourceName()
					+ " does not have a @MultipageEntryPoint annotation");
		}
	}

	private UrlPatternEntryPoint getAnnotation(JClassType classType) {
		return classType.getAnnotation(UrlPatternEntryPoint.class);
	}
}
